package api

import (
	"github.com/gin-gonic/gin"
	"math/rand"
	"monthly-test/global"
	"monthly-test/internal/model/dao"
	"monthly-test/internal/model/service"
	"monthly-test/internal/request"
	"net/http"
	"strconv"
	"strings"
)

func CreateGroup(c *gin.Context) {
	var req request.GroupReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "参数错误"})
		return
	}
	err = service.GetUserById(req.Id)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "用户不存在"})
		return
	}
	group := dao.Group{
		UserId: req.Id,
		Code:   rand.Intn(900000000) + 999999999,
		Name:   req.Name,
	}
	err = global.DB.Create(&group).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "失败"})
		return
	}
	err = global.DB.Where("id = ?", group.ID).First(&group).Error
	member := dao.GroupMember{
		UserId:  int64(req.Id),
		GroupId: int64(int(group.ID)),
		Sign:    1,
	}
	err = global.DB.Create(&member).Error
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "群聊创建成功"})
}

func UpdateStatus(c *gin.Context) {
	var req request.GroupRequest
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "参数错误"})
		return
	}
	var member dao.GroupMember
	//err = global.DB.Model(&member).Count(&member.GroupId).Error
	//if err != nil {
	//	return
	//}
	err = global.DB.Where("group_id = 3").Find(&member).Error
	if err != nil {
		return
	}
	if strings.Count(strconv.Itoa(member.Sign), "2") >= 3 {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "群管理已满"})
		return
	}
	err = global.DB.Model(&member).Where("user_id = ?", req.UserId).Update("sign", req.Sign).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "修改失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "成功"})
}
func Join(c *gin.Context) {
	//只用群主或者管理员
	var req request.Req
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "参数错误"})
		return
	}

	var member dao.GroupMember
	err = global.DB.Where("user_id = ?", req.UserId).First(&member).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "用户不存在"})
		return
	}
	if member.Sign != 1 {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "只用管理员获取群组才能移出群或邀请"})
		return
	}
	groupMember := dao.GroupMember{
		UserId:  int64(req.UserId),
		GroupId: int64(req.GroupId),
	}
	err = global.DB.Create(&groupMember).Error
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "成功"})
}
func GroupList(c *gin.Context) {
	userId := c.Query("userId")
	var member []dao.GroupMember
	err := global.DB.Where("user_id = ?", userId).Order("name desc").Find(&member).Error
	if err != nil {
		return
	}
	var slice []request.Req
	for _, groupMember := range member {
		slice = append(slice, request.Req{
			UserId:  int(groupMember.UserId),
			GroupId: int(groupMember.GroupId),
		})
	}
	c.JSON(http.StatusOK, gin.H{"message": "用户群聊展示成功", "data": slice})

}
